<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app"></div>

  <!-- 添加React的依赖 -->
  <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

  <!-- 开始开发 -->
  <script type="text/babel">
    // 封装APP组件
    class App extends React.Component {
      constructor() {
        super()
        // 定义数据
        this.state = {
          message: 'hello world'
        }
      }

      render() {
        return (
          <div>
            <h2>{this.state.message}</h2>  
            <button onClick={this.btnClick.bind(this)}>改变文本</button>
          </div>
        )
      }

      btnClick() {
        /* 1.为什么这里的 this 是undefined?
            因为React在调用btnClick该方法时,btnClick.apply(undefined)
            所以这里的this是undefined
           2.应该使用state保存响应式数据
            通过setState来修改数据*/
        // this.message = 'hello react'
        // console.log(this)
        this.setState({
          message: 'hello react'
        })
      }


    }


    // 渲染组件
    ReactDOM.render(<App/>, document.getElementById('app'))
  </script>
</body>

</html>